﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Assets.Scripts
{
    class TaskJobTwo : TaskBase<TaskJobTwo>
    {
        private Stopwatch stopWatch = new Stopwatch();
        public override string TaskName
        {
            get { return "TaskJobTwo"; }
        }

        private int _taskId;
        public override int TaskID
        {
            get
            {
                return _taskId;
            }

            set
            {
                _taskId = value;
            }
        }

        private TaskStatus _taskStatus;
        public override TaskStatus taskStatus
        {
            get { return _taskStatus; }
            set
            {
                _taskStatus = value;
                OnStatueChanged(_taskStatus);
            }
        }

        private TimeSpan _eslapsed = new TimeSpan();
        public override TimeSpan Eslapsed
        {
            get { return _eslapsed; }
            set { _eslapsed = value; }
        }

        private bool isCanReverse;
        public override bool IsCanReverse
        {
            get { return IsCanReverse; }
        }

        public override void OnReverse()
        {
            base.OnReverse();
        }

        public override void OnDoTask(CancellationToken cancellationToken)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    taskStatus = TaskStatus.Cancle;
                    return;
                }
                taskStatus = TaskStatus.Start;
                string path = FilePath;

                if (File.Exists(path))
                {
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                        {
                            StreamReader sr = new StreamReader(fs);
                            string data = sr.ReadLine();
                            while (!string.IsNullOrEmpty(data))
                            {
                                _taskStatus = TaskStatus.Running;
                                UnityEngine.Debug.Log(data);
                                data = sr.ReadLine();
                            }
                            sr.Close();
                            fs.Close();
                        }
                        taskStatus = TaskStatus.Success;
                    }

                    if (cancellationToken.IsCancellationRequested)
                    {
                        _taskStatus = TaskStatus.Cancle;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                taskStatus = TaskStatus.Fail;
                throw ex;
            }

        }

        public override void OnFinal()
        {
            stopWatch.Stop();
            _eslapsed = stopWatch.Elapsed;
        }

        public override void OnReady()
        {
            stopWatch.Restart();
        }

        public override void OnStatueChanged(TaskStatus taskStatus)
        {
            switch (taskStatus)
            {
                case TaskStatus.None:
                    break;
                case TaskStatus.Start:
                    OnReady();
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Start,taskId:{0}</color>", _taskId);
                    break;
                case TaskStatus.Running:
                    break;
                case TaskStatus.Success:
                case TaskStatus.Fail:
                    OnFinal();
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Finish,taskId:{0}</color>", _taskId);
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Cost Time:{0}</color>", _eslapsed.TotalSeconds + "s");
                    UnityEngine.Debug.Log("--------------------------------------------------------");
                    break;
                case TaskStatus.Cancle:
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Canclled,taskId:{0}</color>", _taskId);
                    break;
                default:
                    break;
            }
        }
    }
}
